//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
**************************************************************************
*/

/**
* 工具栏插件类；
*/



define(function (require, exports, module) {
	
	FXREADER.RIBBON_TOOLBAR_PLUGIN_NAME = "ToolBar Ribbon Plugin";
	
    FXREADER.CRibbonToolBarPlugin = function (pReaderApp) {
        var _pReaderApp = pReaderApp,
            _bInited = false,
            _Language = _pReaderApp.GetLanguage(),
             _self = this;

        var _RibbonDivID = _pReaderApp.GetTopPanelID()+"_ribbon";
        var _TopBar1DivID = _RibbonDivID + "_topbar1";
        var _TopBar2DivID = _RibbonDivID + "_topbar2";
        var _LeftToolBarDivID = _TopBar2DivID+"_dockButtons";
        var _RightToolBarDivID = _TopBar2DivID+ "_righttools";
        //获取Plugin名称;
        this.GetPluginName = function () {
            return FXREADER.RIBBON_TOOLBAR_PLUGIN_NAME;
        };

        //插件被注册的时候调用;
        this.PIOnRegister = function () {
            if (FXREADER.Config.ToolBar == null)
                return false;
            if (_bInited)
                return true;
            _self.CreateCmdAndCmdUI();
            var jqTopPanel = $("#" + _pReaderApp.GetTopPanelID());
            var ribbonPanelDiv = "<div id='"+_RibbonDivID+"' class='fwr_ribbon_top_panel' />";
            jqTopPanel.append(ribbonPanelDiv);
            var jqRibbonDiv = $("#"+_RibbonDivID);
            var topBar1Div = "<div id='" + _TopBar1DivID + "' class='fwr_ribbon_topbar1'/>";
            jqRibbonDiv.append(topBar1Div);
            var jqTopBar1 = $("#"+_TopBar1DivID);
            var topBar2Div = "<div id='" + _TopBar2DivID + "' class='fwr_ribbon_topbar2'/>";
            jqRibbonDiv.append(topBar2Div);
            var jqTopBar2 = $("#"+_TopBar2DivID);
            
            var fileNameDivID = _TopBar1DivID+ "_filename";
            var fileNameDiv = "<span id='" + fileNameDivID + "' class='fwr_file_name'>" + _pReaderApp.GetFileName() + "</span>" ;
            jqTopBar1.append(fileNameDiv);
            var toolBarButtonDockDiv = "<span class='fwr_ribbon_topbar2_dockbutton fwr_text_upperCase fwr_ribbon_left_toolbar' id='"+_LeftToolBarDivID+"' />";
            jqTopBar2.append(toolBarButtonDockDiv);
            var rightToolBarDiv = "<span class='fwr_ribbon_right_toolbar' id='"+_RightToolBarDivID+"' />";
            jqTopBar2.append(rightToolBarDiv);
            var brandDivId = _TopBar1DivID + "_brand";
            var brandDiv = "<span class='fwr_ribbon_topbar1_container fwr_ribbon_topbar1_rowleft' id='"+brandDivId+"'>" +
            					"<a style='width:16px;height:16px'> <img width='16' height='16' style='border:none; vertical-align:middle;margin-top:-3px;' src='images/foxitappicon.png' /> </a>" +
            					"<span id='FoxitPDFWebReaderBrand' class='fwr_foxit_brand'>Foxit Web Reader</span>"+
            				"</span>";
          
            jqTopBar1.append(brandDiv);
            jqTopPanel.show();
           // jqTopPanel.append(_self.CreateToolBarHtml());
        };

        //模块初始化;
        this.PIInit = function () {
            if (FXREADER.Config.ToolBar == null)
                return false;
            if (_bInited)
                return true;
            var jqRightToolBar = $("#"+_RightToolBarDivID);
            jqRightToolBar.append(_self.CreateRightToolBarHtml());
            var jqLeftToolBar = $("#"+_LeftToolBarDivID);
            jqLeftToolBar.append(_self.CreateLeftToolBarHtml());
            
            _self.InitToolBarUI();
            $('.fwr_toolbar_btn').live('mousedown', function (event) {
                try {
                    if (event.button == 0 || event.button == 1) {
                        var cmdName = event.target.getAttribute("cmd");
                        _pReaderApp.ExecCommand(cmdName, null);
                    }
                }
                catch (ex) {
                	throw new Error("mousedown error");
                }
            });
     //       _self.CreateCmdAndCmdUI();
          /*  var jqTopPanel = $("#" + _pReaderApp.GetTopPanelID());
            jqTopPanel.html("");
            jqTopPanel.append(_self.CreateToolBarHtml());
            _self.InitToolBarUI();

            $('.fwr_toolbar_btn').live('mousedown', function (event) {
                try {
                    if (event.button == 0 || event.button == 1) {
                        var cmdName = event.target.getAttribute("cmd");
                        _pReaderApp.ExecCommand(cmdName, null);
                    }
                }
                catch (ex) {
                }
            });
            _pReaderApp.OnResize();
            _pReaderApp.StepProgress();*/
            _bInited = true;
        };

        this.CreateRightToolBarHtml = function () {
        	
        	var rightTools = FXREADER.Config.RibbonToolBar.right;
        	var rightToolsNum = rightTools.length;
        	var toolbarHtml = "";
        	for(var i = 0; i < rightToolsNum; i++)
        	{
        		 var btnName = rightTools[i];
                 var pCommandUI = _pReaderApp.GetCommandUI(btnName);
                 if (pCommandUI == null)
                     continue;
                 toolbarHtml += pCommandUI.CreateHtml();
        	}
            return toolbarHtml;
        };
        
        this.CreateLeftToolBarHtml = function () {
        	
        	var leftTools = FXREADER.Config.RibbonToolBar.left;
        	var leftToolsNum = leftTools.length;
        	var toolbarHtml = "";
        	for(var i = 0; i < leftToolsNum; i++)
        	{
        		 var btnName = leftTools[i];
                 var pCommandUI = _pReaderApp.GetCommandUI(btnName);
                 if (pCommandUI == null)
                     continue;
                 toolbarHtml += pCommandUI.CreateHtml();
        	}
            return toolbarHtml;
        };
        
        this.InitToolBarUI = function () {
        	var rightTools = FXREADER.Config.RibbonToolBar.right;
        	var rightToolsNum = rightTools.length;
        	for(var i = 0; i < rightToolsNum; i++)
        	{
        		 var btnName = rightTools[i];
                 var pCommandUI = _pReaderApp.GetCommandUI(btnName);
                 if (pCommandUI == null)
                     continue;
                 pCommandUI.Init();
        	}
        	
        	var leftTools = FXREADER.Config.RibbonToolBar.left;
        	var leftToolsNum = leftTools.length;
        	for(var i = 0; i < leftToolsNum; i++)
        	{
        		 var btnName = leftTools[i];
                 var pCommandUI = _pReaderApp.GetCommandUI(btnName);
                 if (pCommandUI == null)
                     continue;
                 pCommandUI.Init();
        	}
        };

        this.CreateCmdAndCmdUI = function () {
            //添加分割符;
            _pReaderApp.AddCommandUI({
                UIName: "ToolBarSeparator",
                CommandName: null,
                CreateHtmlContentFunc: function () {
                    return "<div class='fwr_toolbar_separator'></div>";
                }
            });
            
         // this is a fix for the jQuery slide effects
            function slideToggle(el, bShow){
              var $el = $(el), height = $el.data("originalHeight"), visible = $el.is(":visible");
              
              // if the bShow isn't present, get the current visibility and reverse it
              if( arguments.length == 1 ) bShow = !visible;
              
              // if the current visiblilty is the same as the requested state, cancel
              if( bShow == visible ) return false;
              
              // get the original height
              if( !height ){
                // get original height
                height = $el.show().height();
                // update the height
                $el.data("originalHeight", height);
                // if the element was hidden, hide it again
                if( !visible ) $el.hide().css({height: 0});
              }

              // expand the knowledge (instead of slideDown/Up, use custom animation which applies fix)
              if( bShow ){
                $el.show().animate({height: height}, {duration: 250});
              } else {
                $el.animate({height: 0}, {duration: 250, complete:function (){
                    $el.hide();
                  }
                });
              }
            }
            
            _pReaderApp.AddCommandUI({
                UIName: "QRCode",
                CommandName: null,
                CreateHtmlContentFunc: function () {
                    return "<div class='fwr_toolbar_btn fwr_qrcode_frame fwr_toolbar_btn_margin' id='fx_doc_qrcode' style='padding-left: 0px;padding-right: 0px;'><a class='fwr_qrcode'/></div>";
                },
                Init: function () {
                	var jqMainFrm = $("#" + _pReaderApp.GetMainView().GetMainFrameID());
                	jqMainFrm.append("<div class='fwr_qrcode_image_div fwr_hidden' id='fx_doc_qrcode_image'><img id='fwr_qrcode_image' alt='' src='' class='fwr_qrcode_image '></img></div>");
                	var imgUrl = FXREADER.Config.Url.QrcodeUrl ;//+ "?fileid=" + encodeURIComponent(_pReaderApp.GetFileID());
                	$("#fwr_qrcode_image").attr("src", imgUrl);
                	$("#fx_doc_qrcode").bind('mouseenter', function () {
                		slideToggle("#fx_doc_qrcode_image",true); 
                       return true;
                    });
                	$("#fx_doc_qrcode").bind('mouseleave', function () {
                		
                		slideToggle("#fx_doc_qrcode_image",false); 
                          return true;
                      });
                }
            });
            
            _pReaderApp.AddCommandUI({
                UIName: "FileOpen",
                CommandName: 'FileOpen',
                State: false,
                CreateHtmlContentFunc: function () {
                	 return "<a id='btnFileOpen' style='margin-left: 1px;' class='fwr_toolbar_btn fwr_toobar_fileopen fwr_toolbar_text_btn' cmd=''>"
                     +"<span class='fwr_toollabel' style='color:#fff;'>" + _Language.CommandUI.FileOpen + "</span></a>";           
                },
                OnCommandStateChange: function (data) {
                	
                },
                Init: function () {

                }
            });
        };


        //模块卸载;
        this.PIUnload = function () {
        };

        this.GetReaderApp = function () {
            return _pReaderApp;
        };
    };

    exports.Reader_CreatePlugin = function (pReaderApp) {
        var pToolBarPlugin = new FXREADER.CRibbonToolBarPlugin(pReaderApp);
        return pToolBarPlugin;
    };
});